package com.cskaoyan.market.admin.service.impl;

import com.cskaoyan.market.admin.service.AdminAdminService;
import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.db.domain.MarketAdminExample;
import com.cskaoyan.market.db.mapper.MarketAdminMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminAdminServiceImpl implements AdminAdminService {
    @Autowired
    private MarketAdminMapper adminMapper;
    /**
     * 查询数据
     * @param page 查询页数
     * @param limit 每页条数
     * @param username  查询关键字
     * @param sort 排序列
     * @param order 排序方法
     * @return List<MarketAdmin>
     */
    @Override
    public List<MarketAdmin> list(Integer page, Integer limit, String username, String sort, String order) {
        // 获取映射文件
        
        // 获取编辑器
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        // select id,username,avatar,roleIds from market_admin where username like %username% limit
        // 设定待查询列
        MarketAdmin.Column id = MarketAdmin.Column.id;
        MarketAdmin.Column userName = MarketAdmin.Column.username;
        MarketAdmin.Column avatar = MarketAdmin.Column.avatar;
        MarketAdmin.Column roleIds = MarketAdmin.Column.roleIds;
        // 添加查询条件
        marketAdminExample.setOrderByClause(sort + " " + order);
        MarketAdminExample.Criteria criteria = marketAdminExample.or().andLogicalDeleted(true);
        if (username != null) {
            criteria.andUsernameLike("%" + username + "%");
        }
        // 返回查询数据
        PageHelper.startPage(page, limit);
        List<MarketAdmin> marketAdmins = adminMapper.selectByExampleSelective(marketAdminExample, id, userName, avatar, roleIds);
        
        return marketAdmins;
    }

    /**
     * 创建管理员
     * @param newMarkAdmin
     * @return
     */
    @Override
    public MarketAdmin create(MarketAdmin newMarkAdmin) {

        // 插入数据
        int i = adminMapper.insertSelective(newMarkAdmin);
        // 根据最后插入的id，获取插入的数据
        MarketAdmin marketAdmin = adminMapper.selectByPrimaryKey(i);
        // 收尾
        
        
        return marketAdmin;
    }

    @Override
    public MarketAdmin update(MarketAdmin updateMarkAdmin) {

        // 更新数据
        int i = adminMapper.updateByPrimaryKeySelective(updateMarkAdmin);
        // 根据最后插入的id，获取插入的数据
        MarketAdmin marketAdmin = adminMapper.selectByPrimaryKey(i);
        
        
        return marketAdmin;
    }

    /**
     * 删除管理员
     * @param deleteMarketAdmin 待删除管理员数据
     * @return boolean 是否成功
     */
    @Override
    public boolean delete(MarketAdmin deleteMarketAdmin) {
        // 设定为逻辑删除
        deleteMarketAdmin.setDeleted(true);
        // 更新数据
        int i = adminMapper.updateByPrimaryKeySelective(deleteMarketAdmin);
        // 收尾
        
        
        // 返回判定结果
        return i > 0;
    }
}
